package com.sxds.wn.util;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.security.MessageDigest;

import org.apache.log4j.Logger;



public class StringUtil {

    private final static Logger log = Logger.getLogger(StringUtil.class);

    /**
     * Encode a string using algorithm specified in web.xml and return the
     * resulting encrypted password. If exception, the plain credentials
     * string is returned
     *
     * @param password Password or other credentials to use in authenticating this username
     * @param algorithm Algorithm used to do the digest
     * @return encypted password based on the algorithm.
     */
    public static String encodePassword(String password, String algorithm) {
        byte[] unencodedPassword = password.getBytes();
        MessageDigest md = null;
        try {
            // first create an instance, given the provider
            md = MessageDigest.getInstance(algorithm);
        } catch (Exception e) {
            log.error("Exception: " + e);
            return password;
        }
        md.reset();
        // call the update method one or more times
        // (useful when you don't know the size of your data, eg. stream)
        md.update(unencodedPassword);
        // now calculate the hash
        byte[] encodedPassword = md.digest();
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < encodedPassword.length; i++) {
            if ((encodedPassword[i] & 0xff) < 0x10) {
                buf.append("0");
            }
            buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
        }
        return buf.toString();
    }

    /**
     * Encode a string using Base64 encoding. Used when storing passwords
     * as cookies.
     *
     * This is weak encoding in that anyone can use the decodeString
     * routine to reverse the encoding.
     *
     * @param str
     * @return String
     */
    public static String encodeString(String str)  {
        sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
        return encoder.encodeBuffer(str.getBytes()).trim();
    }

    /**
     * Decode a string using Base64 encoding.
     *
     * @param str
     * @return String
     */
    public static String decodeString(String str) {
        sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();
        try {
            return new String(dec.decodeBuffer(str));
        } catch (IOException io) {
        	throw new RuntimeException(io.getMessage(), io.getCause());
        }
    }
    
    /**
     * parse primitive boolean value from string
     * @param booleanValue "true" or "false"
     * @return true or false
     * @throws Exception parse excpeiton
     */
    public static boolean getBooleanValue(String booleanValue) throws Exception{
    	if( booleanValue==null || "".equals(booleanValue) ) throw new IllegalArgumentException("illegla argument");
		try{
			return Boolean.parseBoolean(booleanValue);
		}catch(Exception ex){
			throw new IllegalArgumentException("illegla argument");
		}
    }
    
    /**
     * 
     * @param booleanValues
     * @return
     * @throws Exception
     */
    public static boolean[] getBooleanArrayValue(String[] booleanValues) throws Exception{
    	if( booleanValues==null || booleanValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	boolean[] arrayValue = new boolean[booleanValues.length];
    	for( int i=0;i<booleanValues.length;i++ ){
    		arrayValue[i] = StringUtil.getBooleanValue(booleanValues[i]);
    	}
    	return arrayValue;
    }
    
    /**
     * 
     * @param booleanValues
     * @return
     * @throws Exception
     */
    public static Object[] getBooleanArrayValueObjects(String[] booleanValues) throws Exception{
    	if( booleanValues==null || booleanValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	Object[] arrayValue = new Object[booleanValues.length];
    	for( int i=0;i<booleanValues.length;i++ ){
    		arrayValue[i] = Boolean.valueOf(StringUtil.getBooleanValue(booleanValues[i]));
    	}
    	return arrayValue;
    }
    
    
    /**
     * parse primitive boolean value from string to wrapper
     * @param booleanValue "true" or "false"
     * @return Boolean.TRUE or Boolean.FALSE
     * @throws Exception parse excpeiton
     */
    public static Boolean getBooleanWrapperValue(String booleanValue) throws Exception{
    	return new Boolean( StringUtil.getBooleanValue(booleanValue) );
    }
    
    /**
     * 
     * @param booleanValues
     * @return
     * @throws Exception
     */
    public static Boolean[] getBooleanWrapperArrayValue(String[] booleanValues) throws Exception{
    	if( booleanValues==null || booleanValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	Boolean[] arrayValue = new Boolean[booleanValues.length];
    	for( int i=0;i<booleanValues.length;i++ ){
    		arrayValue[i] = StringUtil.getBooleanWrapperValue(booleanValues[i]);
    	}
    	return arrayValue;
    }
    
    /**
     * parse from string to int
     * @param intValue intValue include 0-9 number elements
     * @return int value
     * @throws Exception parse process exception
     */
    public static int getIntValue(String intValue) throws Exception{
		if( intValue==null || "".equals(intValue) ) throw new IllegalArgumentException("illegla argument");
		try{
			return Integer.parseInt(intValue);
		}catch(NumberFormatException ex){
			throw new IllegalArgumentException("illegla argument");
		}
	}
    
    /**
     * 
     * @param intValues
     * @return
     * @throws Exception
     */
    public static int[] getIntArrayValue(String[] intValues) throws Exception{
    	if( intValues==null || intValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	int[] arrayValue = new int[intValues.length];
    	for( int i=0;i<intValues.length;i++ ){
    		arrayValue[i] = StringUtil.getIntValue(intValues[i]);
    	}
    	return arrayValue;
    }
    
    /**
     * 
     * @param intValues
     * @return
     * @throws Exception
     */
    public static Object[] getIntArrayValueObjects(String[] intValues) throws Exception{
    	if( intValues==null || intValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	Object[] arrayValue = new Object[intValues.length];
    	for( int i=0;i<intValues.length;i++ ){
    		arrayValue[i] = new Integer(StringUtil.getIntValue(intValues[i]));
    	}
    	return arrayValue;
    }
    
    /**
     * parse from string to int wrapper object
     * @param intValue intValue intValue include 0-9 number elements
     * @return int Primitive Warpper
     * @throws Exception parse process exception
     */
    public static Integer getIntegerWrapperValue(String intValue) throws Exception{
    	return new Integer( StringUtil.getIntValue(intValue) );
    }
    
    /**
     * 
     * @param intValues
     * @return
     * @throws Exception
     */
    public static Integer[] getIntegerWrapperArrayValue(String[] intValues) throws Exception{
    	if( intValues==null || intValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	Integer[] arrayValue = new Integer[intValues.length];
    	for( int i=0;i<intValues.length;i++ ){
    		arrayValue[i] = StringUtil.getIntegerWrapperValue(intValues[i]);
    	}
    	return arrayValue;
    }
	
    /**
     * 
     * @param shortValue
     * @return
     * @throws Exception
     */
	public static short getShortValue(String shortValue) throws Exception{
		if( shortValue==null || "".equals(shortValue) ) throw new IllegalArgumentException("illegla argument");
		try{
			return Short.parseShort(shortValue);
		}catch(NumberFormatException ex){
			throw new IllegalArgumentException("illegla argument");
		}
	}
	
	/**
	 * 
	 * @param shortValues
	 * @return
	 * @throws Exception
	 */
	public static short[] getShortArrayValue(String[] shortValues) throws Exception{
    	if( shortValues==null || shortValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	short[] arrayValue = new short[shortValues.length];
    	for( int i=0;i<shortValues.length;i++ ){
    		arrayValue[i] = StringUtil.getShortValue(shortValues[i]);
    	}
    	return arrayValue;
    }
	
	/**
	 * 
	 * @param shortValues
	 * @return
	 * @throws Exception
	 */
	public static Object[] getShortArrayValueObjects(String[] shortValues) throws Exception{
    	if( shortValues==null || shortValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	Object[] arrayValue = new Object[shortValues.length];
    	for( int i=0;i<shortValues.length;i++ ){
    		arrayValue[i] = new Short(StringUtil.getShortValue(shortValues[i]));
    	}
    	return arrayValue;
    }
	
	/**
	 * 
	 * @param shortValue
	 * @return
	 * @throws Exception
	 */
	public static Short getShortWrapperValue(String shortValue) throws Exception{
		return new Short( StringUtil.getShortValue(shortValue) );
	}
	
	/**
	 * 
	 * @param shortValues
	 * @return
	 * @throws Exception
	 */
	public static Short[] getShortWrapperArrayValue(String[] shortValues) throws Exception{
    	if( shortValues==null || shortValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	Short[] arrayValue = new Short[shortValues.length];
    	for( int i=0;i<shortValues.length;i++ ){
    		arrayValue[i] = StringUtil.getShortWrapperValue(shortValues[i]);
    	}
    	return arrayValue;
    }
	
	/**
	 * 
	 * @param longValue
	 * @return
	 * @throws Exception
	 */
	public static long getLongValue(String longValue) throws Exception{
		if( longValue==null || "".equals(longValue) ) throw new IllegalArgumentException("illegla argument");
		try{
			return Long.parseLong(longValue);
		}catch(NumberFormatException ex){
			throw new IllegalArgumentException("illegla argument");
		}
	}
	
	/**
	 * 
	 * @param longValues
	 * @return
	 * @throws Exception
	 */
	public static long[] getLongArrayValue(String[] longValues) throws Exception{
    	if( longValues==null || longValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	long[] arrayValue = new long[longValues.length];
    	for( int i=0;i<longValues.length;i++ ){
    		arrayValue[i] = StringUtil.getShortValue(longValues[i]);
    	}
    	return arrayValue;
    }	
	
	/**
	 * 
	 * @param longValues
	 * @return
	 * @throws Exception
	 */
	public static Object[] getLongArrayValueObjects(String[] longValues) throws Exception{
    	if( longValues==null || longValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	Object[] arrayValue = new Object[longValues.length];
    	for( int i=0;i<longValues.length;i++ ){
    		arrayValue[i] = new Long(StringUtil.getShortValue(longValues[i]));
    	}
    	return arrayValue;
    }
	
	/**
	 * 
	 * @param longValue
	 * @return
	 * @throws Exception
	 */
	public static Long getLongWrapperValue(String longValue) throws Exception{
		return new Long( StringUtil.getLongValue(longValue) );
	}
	
	/**
	 * 
	 * @param longValues
	 * @return
	 * @throws Exception
	 */
	public static Long[] getLongWrapperArrayValue(String[] longValues) throws Exception{
    	if( longValues==null || longValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	Long[] arrayValue = new Long[longValues.length];
    	for( int i=0;i<longValues.length;i++ ){
    		arrayValue[i] = StringUtil.getLongWrapperValue(longValues[i]);
    	}
    	return arrayValue;
    }
	
	/**
	 * 
	 * @param floatValue
	 * @return
	 * @throws Exception
	 */
	public static float getFloatValue(String floatValue) throws Exception{
		if( floatValue==null || "".equals(floatValue) ) throw new IllegalArgumentException("illegla argument");
		try{
			return Float.parseFloat(floatValue);
		}catch(NumberFormatException ex){
			throw new IllegalArgumentException("illegla argument");
		}
	}
	
	/**
	 * 
	 * @param floatValues
	 * @return
	 * @throws Exception
	 */
	public static float[] getFloatArrayValue(String[] floatValues) throws Exception{
    	if( floatValues==null || floatValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	float[] arrayValue = new float[floatValues.length];
    	for( int i=0;i<floatValues.length;i++ ){
    		arrayValue[i] = StringUtil.getShortValue(floatValues[i]);
    	}
    	return arrayValue;
    }
	
	/**
	 * 
	 * @param floatValues
	 * @return
	 * @throws Exception
	 */
	public static Object[] getFloatArrayValueObjects(String[] floatValues) throws Exception{
    	if( floatValues==null || floatValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	Object[] arrayValue = new Object[floatValues.length];
    	for( int i=0;i<floatValues.length;i++ ){
    		arrayValue[i] = new Float(StringUtil.getShortValue(floatValues[i]));
    	}
    	return arrayValue;
    }
	
	/**
	 * 
	 * @param floatValue
	 * @return
	 * @throws Exception
	 */
	public static Float getFloatWrapperValue(String floatValue) throws Exception {
		return new Float( StringUtil.getFloatValue(floatValue) );
	}
	
	/**
	 * 
	 * @param floatValues
	 * @return
	 * @throws Exception
	 */
	public static Float[] getFloatWrapperArrayValue(String[] floatValues) throws Exception{
    	if( floatValues==null || floatValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	Float[] arrayValue = new Float[floatValues.length];
    	for( int i=0;i<floatValues.length;i++ ){
    		arrayValue[i] = StringUtil.getFloatWrapperValue(floatValues[i]);
    	}
    	return arrayValue;
    }
	
	/**
	 * 
	 * @param doubleValue
	 * @return
	 * @throws Exception
	 */
	public static double getDoubleValue(String doubleValue) throws Exception{
		if( doubleValue==null || "".equals(doubleValue) ) throw new IllegalArgumentException("illegla argument");
		try{
			return Double.parseDouble(doubleValue);
		}catch(NumberFormatException ex){
			throw new IllegalArgumentException("illegla argument");
		}
	}
	
	/**
	 * 
	 * @param doubleValues
	 * @return
	 * @throws Exception
	 */
	public static double[] getDoubleArrayValue(String[] doubleValues) throws Exception{
    	if( doubleValues==null || doubleValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	double[] arrayValue = new double[doubleValues.length];
    	for( int i=0;i<doubleValues.length;i++ ){
    		arrayValue[i] = StringUtil.getDoubleValue(doubleValues[i]);
    	}
    	return arrayValue;
    }
	
	/**
	 * 
	 * @param doubleValues
	 * @return
	 * @throws Exception
	 */
	public static Object[] getDoubleArrayValueObjects(String[] doubleValues) throws Exception{
    	if( doubleValues==null || doubleValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	Object[] arrayValue = new Object[doubleValues.length];
    	for( int i=0;i<doubleValues.length;i++ ){
    		arrayValue[i] = new Double(StringUtil.getDoubleValue(doubleValues[i]));
    	}
    	return arrayValue;
    }
	
	/**
	 * 
	 * @param doubleValue
	 * @return
	 * @throws Exception
	 */
	public static Double getDoubleWrapperValue(String doubleValue) throws Exception{
		return new Double( StringUtil.getDoubleValue(doubleValue) );
	}
	
	/**
	 * 
	 * @param doubleValues
	 * @return
	 * @throws Exception
	 */
	public static Double[] getDoubleWrapperArrayValue(String[] doubleValues) throws Exception{
    	if( doubleValues==null || doubleValues.length==0 ) throw new IllegalArgumentException("illegla argument");
    	Double[] arrayValue = new Double[doubleValues.length];
    	for( int i=0;i<doubleValues.length;i++ ){
    		arrayValue[i] = StringUtil.getDoubleWrapperValue(doubleValues[i]);
    	}
    	return arrayValue;
    }
	
	/**
	 * 获取堆栈内容信息
	 * @param ex
	 * @param stringWriter
	 */
	public static String getStackTrace(Throwable ex){
		ByteArrayOutputStream byteOts = new ByteArrayOutputStream();
		PrintStream ps = new PrintStream(new BufferedOutputStream(byteOts),true);
		ex.printStackTrace(ps);
		ps.flush();
		String stackTrace = byteOts.toString();
		ps.close();
		return stackTrace;
	}
}